package com.example.g5project.controller;

import com.example.g5project.entity.Consultant;
import com.example.g5project.entity.frontEndObject.*;
import com.example.g5project.entity.relation.ConsultInfo;
import com.example.g5project.entity.relation.ConsultantClient;
import com.example.g5project.entity.relation.ConsultantMessages;
import com.example.g5project.entity.relation.HelpMessage;
import com.example.g5project.exception.EntityNotExistException;
import com.example.g5project.others.ResponseValue;
import com.example.g5project.service.AdministratortService;
import com.example.g5project.service.ConsultantService;
import com.example.g5project.service.SupervisorService;
import com.example.g5project.tools.EntityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.ArrayList;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName AdministratorController.java
 * @Description TODO
 * @createTime 2022年03月23日 14:25:00
 */
@Api("管理员接口操作")
@CrossOrigin
@RestController
@RequestMapping(value = "/administrator")
public class AdministratorController {
    private final AdministratortService administratortService;
    private final ConsultantService consultantService;
    private final SupervisorService supervisorService;

    public AdministratorController(AdministratortService administratortService, ConsultantService consultantService, SupervisorService supervisorService) {
        this.administratortService = administratortService;
        this.consultantService = consultantService;
        this.supervisorService = supervisorService;
    }

    @ApiOperation("根据月份返回列表")
    @RequestMapping(method = RequestMethod.GET, value = "/getInfoByMonth/{year}/{month}")
    public ResponseValue<?> getInfoByMonth(@ApiParam(value = "年份", required = true, example = "2022") @PathVariable Integer year, @ApiParam(value = "月份", required = true, example = "1") @PathVariable Integer month) throws ParseException {
        ArrayList<PeopleNumberObject> result = administratortService.getInfoByMonth(year, month);
        return ResponseValue.success(result);
    }

    @ApiOperation("根据日期返回咨询师名字")
    @RequestMapping(method = RequestMethod.GET, value = "/getConsultantInfoByDate/{date}")
    public ResponseValue<?> getConsultantInfoByDate(@ApiParam(value = "日期", required = true, example = "2022-03-24") @PathVariable String date) {
        ArrayList<String> result = administratortService.getConsultantInfoByDate(date);
        return ResponseValue.success(result);
    }

    @ApiOperation("根据日期返回督导名字")
    @RequestMapping(method = RequestMethod.GET, value = "/getSupervisorInfoByDate/{date}")
    public ResponseValue<?> getSupervisorInfoByDate(@ApiParam(value = "日期", required = true, example = "2022-03-24") @PathVariable String date) {
        ArrayList<String> result = administratortService.getSupervisorInfoByDate(date);
        return ResponseValue.success(result);
    }

    @ApiOperation("根据日期和名字添加咨询师到排班表中")
    @RequestMapping(method = RequestMethod.GET, value = "/addConsultantSchedule/{date}/{name}")
    public ResponseValue<?> addConsultantIntoScheduleByName(@ApiParam(value = "日期", required = true, example = "2022-03-24") @PathVariable String date, @ApiParam(value = "名字", required = true, example = "dsd") @PathVariable String name) {
        administratortService.addConsultantIntoScheduleByName(name, date);
        return ResponseValue.success("add consultant success");
    }

    @ApiOperation("根据日期和名字添加督导到排班表中")
    @RequestMapping(method = RequestMethod.GET, value = "/addSupervisorSchedule/{date}/{name}")
    public ResponseValue<?> addSupervisorIntoScheduleByName(@ApiParam(value = "日期", required = true, example = "2022-03-24") @PathVariable String date, @ApiParam(value = "名字", required = true, example = "czh") @PathVariable String name) {
        administratortService.addSupervisorIntoScheduleByName(name, date);
        return ResponseValue.success("add supervisor success");
    }

    @ApiOperation("根据日期和名字删除咨询师")
    @RequestMapping(method = RequestMethod.GET, value = "/deleteConsultantSchedule/{date}/{name}")
    public ResponseValue<?> deleteConsultantFromScheduleByName(@ApiParam(value = "日期", required = true, example = "2022-03-24") @PathVariable String date, @ApiParam(value = "名字", required = true, example = "czh") @PathVariable String name) {
        administratortService.deleteConsultantFromScheduleByName(name, date);
        return ResponseValue.success("delete consultant success");
    }

    @ApiOperation("根据日期和名字删除督导")
    @RequestMapping(method = RequestMethod.GET, value = "/deleteSupervisorSchedule/{date}/{name}")
    public ResponseValue<?> deleteSupervisorFromScheduleByName(@ApiParam(value = "日期", required = true, example = "2022-03-24") @PathVariable String date, @ApiParam(value = "名字", required = true, example = "czh") @PathVariable String name) {
        administratortService.deleteSupervisorFromScheduleByName(name, date);
        return ResponseValue.success("delete supervisor success");
    }

    @ApiOperation("根据ID绑定咨询师和督导")
    @RequestMapping(method = RequestMethod.GET, value = "/bind/{consultant_id}/{supervisor_id}")
    public ResponseValue<?> bindConsultantAndSupervisor(@ApiParam(value = "咨询师ID", required = true, example = "1") @PathVariable Integer consultant_id, @ApiParam(value = "月份", required = true, example = "1") @PathVariable Integer supervisor_id) throws EntityNotExistException {
        administratortService.bindConsultantAndSupervisor(consultant_id, supervisor_id);
        return ResponseValue.success("bind success");
    }

    @ApiOperation("根据咨询师名字获取绑定的督导名字")
    @RequestMapping(method = RequestMethod.GET, value = "/getbindrelationship/{name}/")
    public ResponseValue<?> getBindRelationship(@ApiParam(value = "咨询师姓名", required = true, example = "sd") @PathVariable String name) throws EntityNotExistException {
        System.out.println(name);
        Consultant consultant = consultantService.getCounsultantByName(name);
        System.out.println(consultant.getConsultant_id());
        ArrayList<String> nameList = administratortService.getBindRelationship(consultant.getConsultant_id());
        return ResponseValue.success(nameList);
    }

    @ApiOperation("保存此次聊天记录")
    @RequestMapping(method = RequestMethod.GET, value = "/store")
    public ResponseValue<?> storeConsultantRecord(@RequestBody @Validated ConsultantClient message) {
        return ResponseValue.success("store success");
    }

    @ApiOperation("根据咨询信息获取此次咨询师和用户的聊天记录ID")
    @RequestMapping(method = RequestMethod.GET, value = "/getMessageID/{consultant_id}/{client_id}/{consult_date}")
    public ResponseValue<?> getConsultantMessageID(@PathVariable Integer consultant_id, @PathVariable Integer client_id, @PathVariable String consult_date) {
        Integer id = administratortService.getConsultantMessageID(consultant_id, client_id, consult_date);
        return ResponseValue.success(id);
    }

    @ApiOperation("根据咨询聊天ID获取此次咨询师和用户的聊天记录")
    @RequestMapping(method = RequestMethod.GET, value = "/view/{consult_message_id}")
    public ResponseValue<?> viewConsultantRecord(@PathVariable Integer consult_message_id) {
        ArrayList<ConsultantMessages> messages = administratortService.getMessageContentByID(consult_message_id);
        return ResponseValue.success(messages);
    }

    @ApiOperation("根据咨询聊天ID获取此次咨询师和督导的求助记录, 目前指定是必须先有咨询师和用户聊天，然后才可以启动与督导的求助，否则不可能没有用户咨询师和督导直接就能聊")
    @RequestMapping(method = RequestMethod.GET, value = "/view/{help_message_id}")
    public ResponseValue<?> viewConsultantHelpRecord(@PathVariable Integer help_message_id) {
        ArrayList<HelpMessage> messages = administratortService.getMessageHelpContentByID(help_message_id);
        return ResponseValue.success(messages);
    }


    @ApiOperation("根据咨询聊天ID获取此次咨询师和督导的求助记录, 目前指定是必须先有咨询师和用户聊天，然后才可以启动与督导的求助，否则不可能没有用户咨询师和督导直接就能聊")
    @RequestMapping(method = RequestMethod.GET, value = "/check/{name}")
    public ResponseValue<?> checkIdentityByName(@PathVariable String name) throws EntityNotExistException {
        boolean isConsultant = consultantService.getCounsultantByName(name) != null;
        boolean isSupervisor = supervisorService.getSupervisorByName(name) != null;
        if (isConsultant && !isSupervisor)
            return ResponseValue.success(1);    // 是咨询师
        if (!isConsultant && isSupervisor)
            return ResponseValue.success(2);    // 是督导
        return ResponseValue.success(3);        // 啥也不是
    }


    @ApiOperation("插入咨询消息信息")
    @RequestMapping(method = RequestMethod.GET, value = "/addconsultmessage")
    public ResponseValue<?> addConsultMessage(@RequestBody @Validated ConsultInfo consultInfo) {
        administratortService.addConsultMessage(consultInfo);
        return ResponseValue.success("add success");
    }


    @SneakyThrows
    @ApiOperation("根据咨询聊天ID导出此次聊天记录")
    @RequestMapping(method = RequestMethod.GET, value = "/export/{consult_message_id}")
    public ResponseValue<?> exportExcel(@PathVariable Integer consult_message_id) {
        ArrayList<ConsultantMessages> messages;
        messages = administratortService.getMessageContentByID(consult_message_id);
        HSSFWorkbook wb = administratortService.ExportRecordToExcel(messages);
        String fileName = "心理咨询聊天记录_" + consult_message_id + ".xls";
        File file = new File("D:\\");
        try (OutputStream stream = new FileOutputStream(new File(file, fileName))) {
            wb.write(stream);
            stream.close();
            return ResponseValue.success("export success");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseValue.fail("g", "export failed");
        }
    }

    @ApiOperation("返回所有的咨询聊天内容")
    @RequestMapping(method = RequestMethod.GET, value = "/getallconsultmessage")
    public ResponseValue<?> getAllConsultMessage() {
        EntityUtils entityUtils = new EntityUtils();
        ArrayList<ConsultInfo> arrayList = administratortService.getAllConsultMessage();
        ArrayList<ConsultInfoFrontObject> arrayList2 = new ArrayList<>();
        for (ConsultInfo consultInfo : arrayList) {
            ConsultInfoFrontObject consultInfoFrontObject = new ConsultInfoFrontObject();
            consultInfoFrontObject.setConsult_message_id(consultInfo.getConsult_message_id());
            consultInfoFrontObject.setId(consultInfo.getId());
            consultInfoFrontObject.setConsult_date(consultInfo.getStart_time());
            consultInfoFrontObject.setClient_name(consultInfo.getClient_name());
            consultInfoFrontObject.setEvaluation_content_from_client(consultInfo.getEvaluation_content_from_client());
            consultInfoFrontObject.setConsultant_grade(consultInfo.getConsultant_grade());
            consultInfoFrontObject.setSupervisor_name(consultInfo.getSupervisor_name());
            consultInfoFrontObject.setHold_time(consultInfo.getHold_time());
            consultInfoFrontObject.setEnd_time(consultInfo.getEnd_time());
            arrayList2.add(consultInfoFrontObject);
        }
        return ResponseValue.success(arrayList2);
    }

    @ApiOperation("根据姓名和日期返回咨询聊天内容")
    @RequestMapping(method = RequestMethod.GET, value = "/getconsultmessage/{name}/{date}")
    public ResponseValue<?> getConsultMessage(@PathVariable String name, @PathVariable String date) {
        System.out.println(name);
        ConsultInfo consultInfo = administratortService.getConsultMessage(name.trim(), date.trim());
        ConsultInfoFrontObject consultInfoFrontObject = new ConsultInfoFrontObject();
        consultInfoFrontObject.setConsult_message_id(consultInfo.getConsult_message_id());
        consultInfoFrontObject.setId(consultInfo.getId());
        consultInfoFrontObject.setConsult_date(consultInfo.getStart_time());
        consultInfoFrontObject.setClient_name(consultInfo.getClient_name());
        consultInfoFrontObject.setEvaluation_content_from_client(consultInfo.getEvaluation_content_from_client());
        consultInfoFrontObject.setConsultant_grade(consultInfo.getConsultant_grade());
        consultInfoFrontObject.setSupervisor_name(consultInfo.getSupervisor_name());
        consultInfoFrontObject.setHold_time(consultInfo.getHold_time());
        consultInfoFrontObject.setEnd_time(consultInfo.getEnd_time());
        return ResponseValue.success(consultInfoFrontObject);
    }

    @ApiOperation("插入此次聊天信息")
    @RequestMapping(method = RequestMethod.GET, value = "/addconsultmessageall/")
    public ResponseValue<?> addConsultMessageAll(@RequestBody @Validated ConsultantMessages consultantMessages) {
        administratortService.addConsultMessageAll(consultantMessages);
        return ResponseValue.success("add success");
    }

    @ApiOperation("返回指定数量指定月份的咨询数量")
    @RequestMapping(method = RequestMethod.GET, value = "/getAllConsultNumberLimitK/{month}/{k}")
    public ResponseValue<?> getAllConsultNumberLimitK(@PathVariable String month, @PathVariable Integer k) {
        ArrayList<FigureData> arrayList = administratortService.getAllConsultNumberLimitK(month, k);
        ArrayList<FigureData1impl> result = new ArrayList<>();
        for (FigureData figureData : arrayList) {
            FigureData1impl val = new FigureData1impl();
            val.setNumber(Integer.valueOf(figureData.getNumber()));
            val.setDate("2022-04-" + figureData.getDate() );
            result.add(val);
        }
        return ResponseValue.success(result);
    }

    @ApiOperation("返回指定数量指定天数不同时间点的咨询数量")
    @RequestMapping(method = RequestMethod.GET, value = "/gettodayConsultNumberLimitK/{month}/{day}/{k}")
    public ResponseValue<?> gettodayConsultNumberLimitK(@PathVariable String month, @PathVariable Integer day, @PathVariable Integer k) {
        ArrayList<FigureData2> arrayList = administratortService.gettodayConsultNumberLimitK(month, day, k);
        ArrayList<FigureData2impl> result = new ArrayList<>();
        for (FigureData2 figureData2 : arrayList) {
            FigureData2impl val = new FigureData2impl();
            val.setNumber(Integer.valueOf(figureData2.getNumber()));
            val.setDate(figureData2.getDate() + "点");
            result.add(val);
        }
        return ResponseValue.success(result);
    }

}
